﻿using System;
using System.Linq;
using System.Threading;

namespace Celeriq.Utilities
{
    /// <summary />
    public class AcquireReaderLock : IDisposable
    {
        private ReaderWriterLockSlim m_Lock = null;
        private bool m_Disposed = false;

        /// <summary />
        public AcquireReaderLock(ReaderWriterLockSlim rwl)
        {
            m_Lock = rwl;
            m_Lock.EnterReadLock();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary />
        protected virtual void Dispose(bool disposing)
        {
            if (!m_Disposed)
            {
                if (disposing)
                {
                    m_Lock.ExitReadLock();
                }
            }
            m_Disposed = true;
        }
    }

    /// <summary />
    public class AcquireWriterLock : IDisposable
    {
        private ReaderWriterLockSlim m_Lock = null;
        private bool m_Disposed = false;

        /// <summary />
        public AcquireWriterLock(ReaderWriterLockSlim rwl)
        {
            m_Lock = rwl;
            m_Lock.EnterWriteLock();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary />
        protected virtual void Dispose(bool disposing)
        {
            if (!m_Disposed)
            {
                if (disposing)
                {
                    m_Lock.ExitWriteLock();
                }
            }
            m_Disposed = true;
        }
    }

}